package com.hnluchuan.bmjlearning.web.controller.open;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.bmjlearning.service.CacheService;
import com.hnluchuan.bmjlearning.web.controller.BaseController;

@Controller
@RequestMapping(value = "/open/transfer")
public class TransferController extends BaseController {
	
	@Autowired
	private CacheService cacheService;

	/**
	 * 这个时候域名已经是http://px2.quankechina.com/
	 * 
	 * 如果password对上了，那么也可以。。
	 * @param accessToken
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("saveCookie")
	public String saveCookie(HttpServletResponse response, String accessToken) throws Exception {
		System.out.println("save cookie...");
		Cookie c = new Cookie("Access-Token", accessToken);
		c.setPath("/");
		response.addCookie(c);
		return "redirect:/";
	}

	@ResponseBody
	@RequestMapping("setTokenToCache")
	public String setTokenToCache(String token) throws Exception {
		cacheService.setString(token, token.toString(), (int)TimeUnit.DAYS.toSeconds(1)); // 一天的有效期
		return ok();
	}
}
